#include "Socket_Client.h"

Socket_Client::Socket_Client ()
{
	idSocket=-1;
} 

Socket_Client::Socket_Client (char* machine, int port)
{
	cout << "		Trace: Creation de la socket cliente..."<<endl;
	idSocket = -1;
	struct hostent*    host;
	struct sockaddr_in adr;
	if ((host = gethostbyname (machine)) == NULL)
        {
         	cout<<"		Erreur : La machine ("<<machine<<") est introuvable!!!"<<endl;
        }
	else 
	{
		adr.sin_family = PF_INET;
		adr.sin_port   = htons (port);
		bcopy (host->h_addr_list[0], &(adr.sin_addr.s_addr), host->h_length);
		idSocket = socket (PF_INET, SOCK_STREAM, 0);
		if (idSocket < 0)
		{
			cout<<"		Erreur : Impossible de créer la socket Cliente!!!"<<endl;
		}
		else 
		{
			cout << "		Trace : La socket cliente a été créer avec succés..." << endl;
			if (connect (idSocket,(struct sockaddr *) &adr,sizeof(struct sockaddr)) < 0)
			{
				idSocket = -1;
				cout<<"		Erreur : Impossible de se connecter au serveur!!!"<<endl;
			}
			else
			{
				cout << "		Trace : La socket cliente est connectée au serveur..." << endl;
			}
		}
		
	}
}

Socket_Client::Socket_Client (int ids)
{
	idSocket=ids;
}


void  Socket_Client::disconect()
{
	if (idSocket != -1)
	{
		cout << "		Trace : Deconnexion ..." << endl;
		shutdown (idSocket,2);
		idSocket = -1;
	}
	else 
	{
        	 cout<<"		echec decconection"<<endl;
        }
}

void  Socket_Client::sendFlux (char* flux)
{
	if (idSocket != -1)
	{
		write (idSocket, flux, strlen (flux));
		cout << "Trace : Envoi d'un Flux..." << endl;
        }
	else 
	{
        	 cout<<"echec envoi flux"<<endl;
        }
}

char* Socket_Client::receiveFlux()
{
	 char* fluxRecu = NULL;
	 char* temp = NULL;
	 char  reponse[1024];
	 int   nbCaracteresRecu = 0;
	 if (idSocket != -1)
        {
		cout << "		Trace : Reception d'un flux..." << endl;
		nbCaracteresRecu = read (idSocket, &reponse, 1024);
//                 cout<<nbCaracteresRecu<<endl;
		SocketNonBloquante ();
		while (nbCaracteresRecu > 0 )
		{
			if (fluxRecu == NULL)
			{
				fluxRecu = new char [nbCaracteresRecu + 1];
				if (fluxRecu != NULL)
				{
					memset  (fluxRecu, 0, nbCaracteresRecu + 1);
					strncpy (fluxRecu, reponse, nbCaracteresRecu);
				}
				else 
				{
					cout<<"		Erreur : Erreur de Malloc!!!"<<endl;
					return NULL;
				}
			}
			else 
			{
				temp = fluxRecu;
				fluxRecu = new char [strlen (temp) + nbCaracteresRecu + 1];
				if (fluxRecu != NULL)
				{
					memset  (fluxRecu, 0, strlen (temp) + nbCaracteresRecu + 1);
					strncpy (fluxRecu, temp, strlen (temp));
					strncpy (fluxRecu + strlen (temp), reponse, nbCaracteresRecu);
                                         cout<<"nbcaractere recu"<<nbCaracteresRecu<<endl;

				}
				else 
				{
					cout<<"		Erreur : Erreur de Malloc!!!"<<endl;
					return NULL;
				}
				if (temp != NULL) delete[] temp; 
			}
			memset (reponse, 0, 1024);
			nbCaracteresRecu = read (idSocket, &reponse, 1024);
		}
		SocketNonBloquante ();
	}
	else 
	{
         	 cout<<"		Erreur receipt flux"<<endl;
	}
	return fluxRecu;
}

int   Socket_Client::getidSocket ()
{
	return idSocket;
}

void Socket_Client::SocketNonBloquante ()
{
	if ( fcntl (idSocket, F_SETFL, fcntl (idSocket, F_GETFL) | O_NONBLOCK) < 0 )
	{
		cout<<"		Erreur : Changement de mode (B->NB) de la socket!!!"<<endl;
	}
}

void Socket_Client::SocketBloquante ()
{
	if ( fcntl (idSocket, F_SETFL, fcntl (idSocket, F_GETFL) & ~O_NONBLOCK) < 0 )
	{
		cout<<"		Erreur : Changement de mode (NB->B) de la socket!!!"<<endl;;
	}
}

